<?php
/*
---------------------------------------------------------
														+
名称:易通支付通道										+
														+
控制器名称:YiTongController								+
														+
日期:2017-10-11											+
														+
作者:小翁												+
														+
---------------------------------------------------------
*/
namespace Pay\Controller;

use Common\Common\Api\Order;
use Common\Common\Api\Ordermember;
use Common\Common\Api\Redis\Channel;
use Common\Common\Api\Redis\Product;

class JuxinController extends PayController{
    protected $config;
    protected $key;
    protected $url;
    protected $class;
    public function __construct(){
        parent::__construct();
        $classes = explode("\\",__CLASS__);
        $class = end($classes);
        $this->class = substr($class, 0, stripos($class, "Controller"));
    }

    public function pay($array){
        $this->config = Channel::getInstance()->getById($array['channel']);
        if(!$this->config){
            $this->showmessage('通道错误!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $product = Product::getInstance()->getById(I("request.pay_bankcode",0,"intval"));
        if(!$product){
            $this->showmessage('未找到支付产品!',['code'=>C("THIRDCODES.".$this->class)]);
        }

        $parameter = array(
            'code' => $this->config['code'], // 通道名称
            'title' => $this->config['title'],
            'exchange' => 1, // 金额比例
            'gateway' => $this->config['gateway'],
            'orderid' => createOrderId(),
            'out_trade_id' => I("request.pay_orderid"),
            'body' => 'pay',
            'channel'=>$array
        );

        $return = $this->orderadd( $parameter );
		
		
		$data = array();
		$data['mch_id'] = $this->config['mch_id'];
        $data['out_trade_no'] = $return['orderid'];
		$data['total_fee'] = $return['amount'];
		$data['notify_url'] = $return['notifyurl'];
		$data['return_url'] = $return['callbackurl'];
		
		switch($this->config['paytype']){
			case 1:
			case 2:
				$data['bank_id'] = '962';
			break;
			default:
				$this->showmessage('暂不支持此通道!',['code'=>C("THIRDCODES.".$this->class)]);
			break;
		}
		$data['attach'] = 'wanglianfu';
		$data['body'] = 'vip';
		$data['apply_date'] = date("Y-m-d H:i:s",time());
		
		$data['sign'] = $this->getSign($data);
        po($this->config['gateway'], $data);
    }
	
	private function getSign($data){
		ksort($data);
		
		$str = '';
		foreach($data as $k=>$v){
			if(in_array($k,['sign','rsp_code','rsp_msg','attach','body'])){
				continue;
			}
			
			$str .= "{$k}={$v}&";
		}
		
		$str .= "key=".$this->config['signkey'];
		
		return strtoupper(md5($str));
	}
	
	private function getSign2($data){
		ksort($data);
		
		$str = '';
		foreach($data as $k=>$v){
			if(in_array($k,['sign','rsp_code','rsp_msg','body'])){
				continue;
			}
			
			$str .= "{$k}={$v}&";
		}
		
		$str .= "key=".$this->config['signkey'];
		
		return strtoupper(md5($str));
	}

    //通知
    public function notifyurl(){
        //file_put_contents("a.txt",var_export($_POST,true)."\r\n\r\n");
        //file_put_contents("a.txt",var_export($_GET,true."\r\n\r\n"), FILE_APPEND);
        //file_put_contents("a.txt", file_get_contents('php://input')."\r\n\r\n", FILE_APPEND);
        //file_put_contents("a.txt", date('Y-m-d H:i:s',time())."\r\n\r\n", FILE_APPEND);
        //file_put_contents("a.txt",var_export($_POST,true)."\r\n");
        //file_put_contents("a.txt",date("Y-m-d H:i:s",time()) . "\r\n", FILE_APPEND);
		
		//$this->debug_callback();
		
		$p = I("post.");
		
		/* $p = array (
		  'mch_id' => '10037',
		  'order_no' => '201902041757169948549259',
		  'out_trade_no' => '2019020417571457779',
		  'total_fee' => '100.00',
		  'datetime' => '2019-02-04 18:02:44',
		  'returncode' => '0',
		  'attach' => 'wanglianfu',
		  'sign' => 'DFED6ABE06E6DE284A4BEE058EDF981C',
		); */
		
        //获取订单信息
        $orderid = $p['out_trade_no'];
        $find = $this->checkOrderNotify($orderid);

        if(!$find){
            echo 'order_not_found'; exit;
        }
        //保存回调数据
        M("Order")->where(array("pay_orderid"=>$p['busi_order_no']))->save(array(

			"pay_orderid_rd"=> htmlspecialchars($p['order_no'])
		));

        //获取渠道信息
        $this->config = M("Channel")->where(array('code'=>$this->class))->find();
		
        $sign = $p['sign'];
        $sign2 = $this->getSign2($p);

        if($sign !== $sign2){
            exit("sign_wrong");
        }
		
        if($p['returncode'] !== '0'){
            exit("pay_fail");
        }
		
        $this->async($find['pay_orderid'],'ok');
    }

    public function callBackUrl(){

        echo '支付成功'; exit;
    }
}